//
//  ViewController.m
//  Test
//
//  Created by 冯思 on 2019/6/18.
//  Copyright © 2019 冯思. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic ,strong)NSString *firstName;
@property (nonatomic ,copy)NSString *secondName;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    //串行队列创建
//  dispatch_queue_t queue = dispatch_queue_create("queueTest", DISPATCH_QUEUE_SERIAL);//串行队列
//    //获取全局队列
//   dispatch_queue_t queue1= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
//    [self syncTest];
//    [self asycTest];
    [self syncMain];
//    [self testKindAndMemberMethod];
//    [self testMaopao];
    NSLog(@"1");
}

-(void)testMaopao{
    int a[] = {900, 2, 3, -58, 34, 76, 32, 43, 56, -70, 35, -234, 532, 543, 2500};
    int n;  //存放数组a中元素的个数
    int i;  //比较的轮数
    int j;  //每轮比较的次数
    int buf;  //交换数据时用于存放中间数据
    n = sizeof(a) / sizeof(a[0]);  /*a[0]是int型, 占4字节, 所以总的字节数除以4等于元素的个数*/
    for (i=0; i<n-1; ++i)  //比较n-1轮
    {
        for (j=0; j<n-1-i; ++j)  //每轮比较n-1-i次,
        {
            if (a[j] < a[j+1])
            {
                buf = a[j];
                a[j] = a[j+1];
                a[j+1] = buf;
            }
        }
    }
    for (i=0; i<n; ++i)
    {
        printf("%d\x20", a[i]);
    }
}

-(void)testRuntime
{
    
}

-(void)testString{
    NSMutableString *str1 = [NSMutableString stringWithFormat:@"李博"];
    self.firstName = str1;
    NSLog(@"使用strong第一次得到的名字：%@",self.firstName);
    
    [str1 appendString:@"爵"];
    NSLog(@"使用strong第二次得到的名字：%@",self.firstName);
    
    NSMutableString *str2 = [NSMutableString stringWithFormat:@"李博"];
    self.secondName = str2;
    NSLog(@"使用copy第一次得到的名字：%@",self.secondName);
    
    [str2 appendString:@"爵"];
    NSLog(@"使用copy第二次得到的名字：%@",self.secondName);
}



-(void)testKindAndMemberMethod
{
    NSMutableString *str=[@"你好" mutableCopy];
//    BOOL isStringKind=[str isKindOfClass:[NSObject class]];
    BOOL isStringMember=[str isMemberOfClass:[NSMutableString class]];
    NSLog(@"%d--%d",isStringMember,isStringMember);
    
//        UIView *v=[UIView new];
//        BOOL isStringMember=[v isMemberOfClass:[UIView class]];
//        NSLog(@"%d--%d",isStringMember,isStringMember);
}


//以下会造成死锁
- (void)syncMain {
    
//    dispatch_queue_t queue = dispatch_get_main_queue();
//    dispatch_sync(queue, ^{
//        NSLog(@"执行任务2");
//    });
//
//    NSLog(@"执行任务3");
}

-(void)syncTest
{
    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"syncConcurrent---begin");
    
    dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"1---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    dispatch_sync(queue, ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"2---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    dispatch_sync(queue, ^{
        // 追加任务3
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"3---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    NSLog(@"syncConcurrent---end");
}

-(void)asycTest{
    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当前线程
    NSLog(@"asyncConcurrent---begin");
    
    dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        // 追加任务1
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"1---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    dispatch_async(queue, ^{
        // 追加任务2
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"2---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    dispatch_async(queue, ^{
        // 追加任务3
        for (int i = 0; i < 2; ++i) {
            [NSThread sleepForTimeInterval:2];              // 模拟耗时操作
            NSLog(@"3---%@",[NSThread currentThread]);      // 打印当前线程
        }
    });
    
    NSLog(@"asyncConcurrent---end");
}


@end
